<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>replace</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Strings</a> &gt; <a href=
    "replace.html">replace</a>
  </div>

  <div class="name-format">
    replace
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string&gt;
  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num, const string&amp; str );
  string&amp; replace( <strong>size_type</strong> index1, <strong>size_type</strong> num1, const string&amp; str, <strong>size_type</strong> index2, <strong>size_type</strong> num2 );
  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num, const char* str );
  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, const char* str, <strong>size_type</strong> num2 );
  string&amp; replace( <strong>size_type</strong> index, <strong>size_type</strong> num1, <strong>size_type</strong> num2, char ch );
  string&amp; replace( iterator start, iterator end, const string&amp; str );
  string&amp; replace( iterator start, iterator end, const char* str );
  string&amp; replace( iterator start, iterator end, const char* str, <strong>size_type</strong> num );
  string&amp; replace( iterator start, iterator end, <strong>size_type</strong> num, char ch );
</pre>

  <p>The function replace() either:</p>

  <ul>
    <li>replaces characters of the current string with up to
    <em>num</em> characters from <em>str</em>, beginning at
    <em>index</em>,</li>

    <li>replaces up to <em>num1</em> characters of the current string
    (starting at <em>index1</em>) with up to <em>num2</em> characters
    from <em>str</em> beginning at <em>index2</em>,</li>

    <li>replaces up to <em>num</em> characters of the current string
    with characters from <em>str</em>, beginning at <em>index</em> in
    <em>str</em>,</li>

    <li>replaces up to <em>num1</em> characters in the current string
    (beginning at <em>index1</em>) with <em>num2</em> characters from
    <em>str</em> beginning at <em>index2</em>,</li>

    <li>replaces up to <em>num1</em> characters in the current string
    (beginning at <em>index</em>) with <em>num2</em> copies of
    <em>ch</em>,</li>

    <li>replaces the characters in the current string from
    <em>start</em> to <em>end</em> with <em>str</em>,</li>

    <li>replaces characters in the current string from <em>start</em>
    to <em>end</em> with <em>num</em> characters from
    <em>str</em>,</li>

    <li>or replaces the characters in the current string from
    <em>start</em> to <em>end</em> with <em>num</em> copies of
    <em>ch</em>.</li>
  </ul>

  <p>For example, the following code displays the string &quot;They say
  he carved it himself...find your soul-mate, Homer.&quot;</p>
  <pre class="example-code">
   string s = &quot;They say he carved it himself...from a BIGGER spoon&quot;;
   string s2 = &quot;find your soul-mate, Homer.&quot;;
   s.replace( 32, s2.length(), s2 );
   cout &lt;&lt; s &lt;&lt; endl;               
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="insert.html">insert</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
